<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="initial-scale=1.0, user-scalable=no, width=device-width"
    />
    <title>规划路线</title>
    <style type="text/css">
      html,
      body,
      #container {
        width: 100%; 
        height: 100%;
      }
    </style>
    <style type="text/css">
      #panel {
        position: fixed;
        background-color: white;
        max-height: 90%;
        overflow-y: auto;
        top: 10px;
        right: 10px;
        width: 280px;
      }
      #panel .amap-call {
        background-color: #009cf9;
        border-top-left-radius: 4px;
        border-top-right-radius: 4px;
      }
      #panel .amap-lib-driving {
        border-bottom-left-radius: 4px;
        border-bottom-right-radius: 4px;
        overflow: hidden;
      }
    </style>
    <link
      rel="stylesheet"
      href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"
    />
    <link href="./layui/css/layui.css" rel="stylesheet" />
    <script src="./layui/layui.js"></script>
    <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
    <script type="text/javascript">
      window._AMapSecurityConfig = {
        securityJsCode: "3e7f8302fb7257062e510caa8e839c2f",
      };
    </script>
    <script src="https://webapi.amap.com/maps?v=2.0&key=2811e6b7060883d17411cfd1edd2dd0e&plugin=AMap.Scale,AMap.HawkEye,AMap.ToolBar,AMap.ControlBar,AMap.Driving,AMap.Walking,AMap.Transfer"></script>

    <script
      type="text/javascript"
      src="https://cache.amap.com/lbs/static/addToolbar.js"
    ></script>
  </head>
  <body>
    <div style="position: absolute; top: 20px; left: 20px; z-index: 999">
      <button class="layui-btn" onclick="goTo()">模拟导航</button>
    </div>
    <div id="container"></div>
    <div id="panel"></div>
    <script type="text/javascript">
      function getQueryString(name) {
        let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        let r = window.location.search.substr(1).match(reg);
        if (r != null) {
          return decodeURIComponent(r[2]);
        }
        return null;
      }
      //基本地图加载
      var map = new AMap.Map("container", {
        resizeEnable: true,
        center: [116.397428, 39.90923], //地图中心点
        zoom: 13, //地图显示的缩放级别
      });
      //构造路线导航类
      var driving = new AMap.Driving({
        map: map,
        panel: "panel",
      });
      // 根据起终点经纬度规划驾车导航路线
      var arr = [];
      driving.search(
        new AMap.LngLat(
          Number(getQueryString("origin").split(",")[0]),
          Number(getQueryString("origin").split(",")[1])
        ),
        new AMap.LngLat(
          Number(getQueryString("destination").split(",")[0]),
          Number(getQueryString("destination").split(",")[1])
        ),
        function (status, result) {
          // result 即是对应的驾车导航信息，相关数据结构文档请参考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
          if (status === "complete") {
            log.success("绘制驾车路线完成");
            for (let i in result.routes[0].steps) {
              let path = result.routes[0].steps[i].path;
              arr = arr.concat(path.map((v) => [v.lng, v.lat]));
            }
          } else {
            log.error("获取驾车数据失败：" + result);
          }
        }
      );
      const goTo = () => {
        // JSAPI2.0 使用覆盖物动画必须先加载动画插件
        AMap.plugin("AMap.MoveAnimation", function () {
          var marker,
            lineArr = arr;
          marker = new AMap.Marker({
            map: map,
            position: arr[0],
            icon: "https://a.amap.com/jsapi_demos/static/demo-center-v2/car.png",
            offset: new AMap.Pixel(-13, -26),
          });

          // 绘制轨迹
          var polyline = new AMap.Polyline({
            map: map,
            path: lineArr,
            showDir: true,
            strokeColor: "#28F", //线颜色
            // strokeOpacity: 1,     //线透明度
            strokeWeight: 6, //线宽
            // strokeStyle: "solid"  //线样式
          });

          var passedPolyline = new AMap.Polyline({
            map: map,
            strokeColor: "#AF5", //线颜色
            strokeWeight: 6, //线宽
          });

          marker.on("moving", function (e) {
            passedPolyline.setPath(e.passedPath);
            map.setCenter(e.target.getPosition(), true);
          });

          marker.moveAlong(lineArr, {
            // 每一段的时长
            duration: 500, //可根据实际采集时间间隔设置
            // JSAPI2.0 是否延道路自动设置角度在 moveAlong 里设置
            autoRotation: true,
          });
        });
      };
    </script>
  </body>
  <style>
    .amap-call {
      display: none;
    }
  </style>
</html>
